package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.AddDeliveryVo;
import com.woniuxy.oldmanapi.cvo.DeliveryDetailVo;
import com.woniuxy.oldmanapi.cvo.DeliveryVo;
import com.woniuxy.oldmanapi.entity.Card;
import com.woniuxy.oldmanapi.entity.Delivery;
import com.woniuxy.oldmanapi.mapper.CardMapper;
import com.woniuxy.oldmanapi.mapper.DeliveryDetailMapper;
import com.woniuxy.oldmanapi.mapper.DeliveryMapper;
import com.woniuxy.oldmanapi.mapper.MedicineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
public class DeliveryService {
    @Autowired
    DeliveryMapper deliveryMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    DeliveryDetailMapper deliveryDetailMapper;
    @Autowired
    CardMapper cardMapper;

    public PageInfo<DeliveryVo> findDeliverysByCons(String oldername, int state, int size, int now) {
        PageHelper.startPage(now,size);
        List<DeliveryVo> list=deliveryMapper.selectDeliVerysByCon(state,oldername);
        return new PageInfo<>(list);
    }

  //作废
    @Transactional
    public void cancelDelivery(int id) {
        //判断:1状态，交费；库存添加；金额---0状态,交费
        Delivery delivery = deliveryMapper.selectByDId(id);
        if(delivery.getState()==1){
            deliveryMapper.cancelDelivery(id);
            List<DeliveryDetailVo> deliveryDetailVos = deliveryDetailMapper.selectByDid(id);
            for(DeliveryDetailVo d:deliveryDetailVos){
                Integer id1 = d.getMedicine().getId();
                Integer count = d.getCount();
                medicineMapper.backaddScoreCount(id1,count);
            }
            cardMapper.addMoneyByNumAndpay(delivery.getCardnum(),delivery.getTotalprice());
        }else if(delivery.getState()==0){
            deliveryMapper.cancelDelivery(id);
        }

    }

    //出库
    @Transactional
    public void DeliveryById(int id, BigDecimal rechargePay,String cardNumber) {
        //判断余额,卡的状态；修改订单状态；修改卡里余额;判断库存；改库存
        Card card=cardMapper.selectByCardId(cardNumber);
      Long balance = card.getBalance().longValue();
      Long pay=rechargePay.longValue();
       if(balance<pay){
           throw  new RuntimeException("余额不足");
       }
       if(card.getStatus()!=1){
           throw  new RuntimeException("此卡已被冻结或挂失，非正常状态，不可用！");
       }
       // Delivery delivery = deliveryMapper.selectByPrimaryKey(id);
        int state=1;
        deliveryMapper.updatePay(id,state,rechargePay,cardNumber);
        cardMapper.updateMoneyByNumAndpay(cardNumber,rechargePay);
        List<DeliveryDetailVo> deliveryDetailVos = deliveryDetailMapper.selectByDid(id);
        for(DeliveryDetailVo d:deliveryDetailVos){
            Integer id1 = d.getMedicine().getId();
            Integer count = d.getCount();
            int i = medicineMapper.loseScoreCount(id1, count);
            if(i<1){
            throw  new RuntimeException("【"+d.getMedicine().getDrugname()+"】库存不足");
            }
        }

    }
//添加出库单
    @Transactional
    public void addDelivery(AddDeliveryVo addDeliveryVo) {
        //1.生成订单编号；2.计算总价；3.state===1.添加出库订单；添加出库明细表
        Delivery delivery=new Delivery();
        String s = validateCode();
        delivery.setOrdernum(s);
        delivery.setDesc(addDeliveryVo.getDesc());
        delivery.setPresentmoney(new BigDecimal(0));
        delivery.setOlderId(addDeliveryVo.getOlderId());
        delivery.setRechargemoney(new BigDecimal(0));
        delivery.setSaletime(addDeliveryVo.getSaletime());
        delivery.setState(0);
        delivery.setTotalprice(addDeliveryVo.getTotalprice());
        deliveryMapper.insertOfdelivery(delivery);

        Date createtime=delivery.getSaletime();
        int orderid=delivery.getId();
        List<DeliveryDetailVo> list = addDeliveryVo.getDeliveryDetailVos();
        deliveryDetailMapper.insetDetailsOfDetail(createtime,orderid,list);


    }

    //生成订单编号
    private String validateCode(){
        String data = "0123456789" ;
        String str = "WNdelivery";
        Random random = new Random();
        for (int i=0;i<5;i++){
            //获取 0 --9 随机数
            int k = random.nextInt(10);
            str += data.charAt(k);
        }
        return str ;
    }

    //修改出库单
    @Transactional
    public void modifyDelivery(AddDeliveryVo addDeliveryVo) {
        //修改订单；删除订单明细；添加所有明细
        Delivery delivery=new Delivery();
        delivery.setId(addDeliveryVo.getDeliveryId());
        delivery.setOrdernum(addDeliveryVo.getOrdernum());
        delivery.setDesc(addDeliveryVo.getDesc());
        delivery.setPresentmoney(new BigDecimal(0));
        delivery.setOlderId(addDeliveryVo.getOlderId());
        delivery.setRechargemoney(new BigDecimal(0));
        delivery.setSaletime(addDeliveryVo.getSaletime());
        delivery.setState(0);
        delivery.setTotalprice(addDeliveryVo.getTotalprice());

        deliveryMapper.updateOfdelivery(delivery);

        Date createtime=delivery.getSaletime();
        int deliveryId=delivery.getId();

        deliveryDetailMapper.deleteByDid(deliveryId);
        List<DeliveryDetailVo> list = addDeliveryVo.getDeliveryDetailVos();
        deliveryDetailMapper.insetDetailsOfDetail(createtime,deliveryId,list);
    }
}
